/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;



import Conexion.Conexion;
import Datos.Camion;
import Datos.Carga;
import Negocio.Negocio;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Mufase
 */
public class RegistroCamiones extends javax.swing.JDialog {
   
    public RegistroCamiones(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        llenarComboboxAuxiliar();
        llenarComboboxConductor();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtPatente = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtCapacidad = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtModelo = new javax.swing.JTextField();
        btnIngresar = new javax.swing.JButton();
        btnConsultar = new javax.swing.JButton();
        btnVolver = new javax.swing.JButton();
        cmbConductor = new javax.swing.JComboBox();
        cmbAuxiliar = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registro de Camiones");
        setResizable(false);

        jLabel1.setText("Patente:");

        jLabel2.setText("Conductor:");

        jLabel3.setText("Auxiliar:");

        txtPatente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPatenteKeyTyped(evt);
            }
        });

        jLabel5.setText("Capacidad:");

        txtCapacidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCapacidadKeyTyped(evt);
            }
        });

        jLabel6.setText("Modelo:");

        txtModelo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtModeloKeyTyped(evt);
            }
        });

        btnIngresar.setText("Ingresar");
        btnIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresarActionPerformed(evt);
            }
        });

        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        btnVolver.setText("Volver");
        btnVolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVolverActionPerformed(evt);
            }
        });

        jLabel4.setText("Kg.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnIngresar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnConsultar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnVolver))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtModelo, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(cmbConductor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cmbAuxiliar, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4))
                            .addComponent(txtPatente, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtPatente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtCapacidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbConductor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(cmbAuxiliar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnIngresar)
                    .addComponent(btnConsultar)
                    .addComponent(btnVolver))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresarActionPerformed
       
         if (this.txtPatente.getText().trim().isEmpty()) {
        JOptionPane.showMessageDialog(this, "Debe Ingresar la patente", "Error de Ingreso", JOptionPane.ERROR_MESSAGE);
        this.txtPatente.requestFocus();
        return;
        }
        if (this.txtCapacidad.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Debe Ingresar la capacidad", "Error de Ingreso", JOptionPane.ERROR_MESSAGE);
            this.txtCapacidad.requestFocus();
            return;
        }
        if (this.txtModelo.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Debe Ingresar el modelo", "Error de Ingreso", JOptionPane.ERROR_MESSAGE);
            this.txtModelo.requestFocus();
            return;
        }
        
        if (this.cmbConductor.getSelectedItem()=="Seleccione...") {
            JOptionPane.showMessageDialog(this, "Debe Ingresar el conductor", "Error de Ingreso", JOptionPane.ERROR_MESSAGE);
            this.cmbConductor.requestFocus();
            return;
        }
        
     if (this.cmbAuxiliar.getSelectedItem()=="Seleccione...") {
            JOptionPane.showMessageDialog(this, "Debe Ingresar el auxiliar", "Error de Ingreso", JOptionPane.ERROR_MESSAGE);
            this.cmbAuxiliar.requestFocus();
            return;
        }           

        
        Negocio neg=new Negocio();
        Camion cam=new Camion();
        cam.setPatente(this.txtPatente.getText().trim());
        cam.setCapacidad(Double.parseDouble(this.txtCapacidad.getText().trim()));
        cam.setModelo(this.txtModelo.getText().trim());
        cam.setConductor(String.valueOf(this.cmbConductor.getSelectedItem()));
        cam.setAux(String.valueOf(this.cmbAuxiliar.getSelectedItem()));
        
        neg.ingresarCamion(cam);
        
    }//GEN-LAST:event_btnIngresarActionPerformed

    private void btnVolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVolverActionPerformed
       int opcion = 0;
        opcion = JOptionPane.showConfirmDialog(this, "¿Desea volver al Menu Principal?", "Salir de Registro de Camiones",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (opcion == JOptionPane.YES_OPTION) {
            this.dispose();
        }
    }//GEN-LAST:event_btnVolverActionPerformed

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
       Negocio neg=new Negocio();
       
        Camion cam=neg.buscarCamion(this.txtPatente.getText().trim());
        
        if(cam.getPatente().trim().length() !=0){
            this.txtPatente.setText(cam.getPatente());
            this.txtCapacidad.setText(String.valueOf(cam.getCapacidad()));
            this.txtModelo.setText(cam.getModelo());
            this.cmbConductor.setSelectedItem(cam.getConductor());
            this.cmbAuxiliar.setSelectedItem(cam.getAux());
        }
        else
        {
        JOptionPane.showMessageDialog(this,"Camion no existe","Error de Ingreso", JOptionPane.ERROR_MESSAGE);
        this.txtPatente.requestFocus();
        }
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void txtPatenteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPatenteKeyTyped
        if (this.txtPatente.getText().trim().length() > 7) {
            evt.consume();
        }
    }//GEN-LAST:event_txtPatenteKeyTyped

    private void txtCapacidadKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCapacidadKeyTyped
        if (this.txtCapacidad.getText().trim().length() > 3) {
            evt.consume();
        }
    }//GEN-LAST:event_txtCapacidadKeyTyped

    private void txtModeloKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtModeloKeyTyped
      if (this.txtModelo.getText().trim().length() > 9) {
            evt.consume();
        }
    }//GEN-LAST:event_txtModeloKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RegistroCamiones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RegistroCamiones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RegistroCamiones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RegistroCamiones.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                RegistroCamiones dialog = new RegistroCamiones(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnIngresar;
    private javax.swing.JButton btnVolver;
    private javax.swing.JComboBox cmbAuxiliar;
    private javax.swing.JComboBox cmbConductor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField txtCapacidad;
    private javax.swing.JTextField txtModelo;
    private javax.swing.JTextField txtPatente;
    // End of variables declaration//GEN-END:variables

   public void llenarComboboxAuxiliar(){
    
        Conexion cnn=new Conexion();
        
        cnn.setDriver("com.mysql.jdbc.Driver");
        cnn.setUrl("jdbc:mysql://localhost:3306/flota");

        cnn.setNombreBaseDatos("Flota");
        cnn.setNombreTabla("Auxiliar");
        cnn.setPassword("");
        cnn.setUser("root");
         
         cnn.setEsSelect(true);

         cnn.setSentenciaSQL("select idAuxiliar from " + cnn.getNombreTabla());
                            

         cnn.conectar();
            cmbAuxiliar.addItem("Seleccione...");                                  
        try {        
            while(cnn.getRst().next()) {
              
                   cmbAuxiliar.addItem(cnn.getRst().getString("idAuxiliar"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(RegistroCamiones.class.getName()).log(Level.SEVERE, null, ex);
        }
        
         cnn.cerrarConexion();
     }
       public void llenarComboboxConductor(){
    
        Conexion cnn=new Conexion();
        
        cnn.setDriver("com.mysql.jdbc.Driver");
        cnn.setUrl("jdbc:mysql://localhost:3306/flota");

        cnn.setNombreBaseDatos("Flota");
        cnn.setNombreTabla("Conductor");
        cnn.setPassword("");
        cnn.setUser("root");
         
         cnn.setEsSelect(true);

         cnn.setSentenciaSQL("select idConductor from " + cnn.getNombreTabla());

         cnn.conectar();
            cmbConductor.addItem("Seleccione...");                                  
        try {        
            while(cnn.getRst().next()) {
              
                   cmbConductor.addItem(cnn.getRst().getString("idConductor"));       
            }
        } catch (SQLException ex) {
            Logger.getLogger(RegistroCamiones.class.getName()).log(Level.SEVERE, null, ex);
        }
        
         cnn.cerrarConexion();
     }
}